package com.chuhe.starry.controller;

import com.chuhe.common.constans.MyConstants;
import com.chuhe.common.domain.dto.SysSettingDto;
import com.chuhe.common.domain.vo.resp.ApiResult;
import com.chuhe.common.domain.vo.resp.SysSettingResp;
import com.chuhe.common.utils.AppConfig;
import com.chuhe.common.utils.RedisComponet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author: 楚河
 * @Date: 2025年05月18日14:56
 */
@RestController
@RequestMapping("/admin")
@Api(value = "系统设置接口", tags = "系统设置相关API", description = "系统设置相关API")
public class AdminSettingController {
    @Autowired
    private AppConfig appConfig;
    @Autowired
    private RedisComponet redisComponet;

    @RequestMapping("/saveSysSetting")
    @ApiOperation("保存设置")
    public ApiResult<?> saveSysSetting(SysSettingDto sysSettingDto,
                                       MultipartFile robotFile,
                                       MultipartFile robotCover) throws IOException {
        if (robotFile != null) {
            String baseFolder = appConfig.getProjectFolder() + MyConstants.FILE_FOLDER_FILE;
            File targetFileFolder = new File(baseFolder + MyConstants.FILE_FOLDER_AVATAR_NAME);
            if (!targetFileFolder.exists()) {
                targetFileFolder.mkdirs();
            }
            String filePath = targetFileFolder.getPath() + "/" + MyConstants.ROBOT_UID + MyConstants.IMAGE_SUFFIX;
            robotFile.transferTo(new File(filePath));
            robotCover.transferTo(new File(filePath + MyConstants.COVER_IMAGE_SUFFIX));
        }
        redisComponet.saveSysSetting(sysSettingDto);
        return ApiResult.success(null);
    }

    @RequestMapping("/getSysSetting")
    @ApiOperation("获取设置")
    public ApiResult<SysSettingResp> getSysSetting() {
        SysSettingResp sysSettingDto = redisComponet.getSysSetting();
        return ApiResult.success(sysSettingDto);
    }
}
